<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加会员</title>
    <link rel="stylesheet" href="/app/admin/component/layui/css/layui.css">
    <style>
        .form-content {
            padding: 20px;
        }
        .layui-form-required:before {
            content: "*";
            color: red;
            margin-right: 4px;
        }
        .form-row {
            margin: 0 -10px;
            display: flex;
            flex-wrap: wrap;
        }
        .form-col {
            width: 50%;
            padding: 0 10px;
            box-sizing: border-box;
        }
    </style>
</head>
<body>
<div class="form-content">
    <form class="layui-form" lay-filter="memberForm">
        <div class="form-row">
            <div class="form-col">
                <div class="layui-form-item">
                    <label class="layui-form-label">会员卡号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="card_no" id="card_no" required lay-verify="required" placeholder="请选择会员卡" autocomplete="off" class="layui-input" >
                        <button type="button" class="layui-btn layui-btn-normal" id="selectCard">选择会员卡</button>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">会员姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" required lay-verify="required" placeholder="请输入会员姓名" autocomplete="off" class="layui-input" style="width: 200px;">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">手机号码</label>
                    <div class="layui-input-block">
                        <input type="text" name="phone" required lay-verify="required|phone" placeholder="请输入会员手机号" autocomplete="off" class="layui-input" style="width: 200px;">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">生日</label>
                    <div class="layui-input-block">
                        <input type="text" name="birthday" id="birthday" placeholder="选择生日日期" autocomplete="off" class="layui-input" style="width: 200px;">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">会员等级</label>
                    <div class="layui-input-block">
                        <select name="level" required lay-verify="required" lay-filter="level" style="width: 200px;">
                            <option value="">请选择会员等级</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                        <input type="radio" name="gender" value="1" title="男">
                        <input type="radio" name="gender" value="2" title="女">
                        <input type="radio" name="gender" value="0" title="未知" checked>
                    </div>
                </div>
            </div>

            <div class="form-col">
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">所属门店</label>
                    <div class="layui-input-block">
                        <select name="store_id" required lay-verify="required" style="width: 200px;">
                            <option value="">请选择所属门店</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">办卡员工</label>
                    <div class="layui-input-block">
                        <select name="staff_id" required lay-verify="required" style="width: 200px;">
                            <option value="">请选择办卡员工</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">地址</label>
                    <div class="layui-input-block">
                        <input type="text" name="address" placeholder="请输入会员地址" autocomplete="off" class="layui-input" style="width: 200px;">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-block">
                        <input type="text" name="email" placeholder="请输入邮箱地址" autocomplete="off" class="layui-input" style="width: 200px;">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea name="remark" placeholder="请输入备注信息" class="layui-textarea" style="width: 200px;"></textarea>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px;">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="memberForm">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<script src="/app/admin/component/layui/layui.js"></script>
<script>
layui.config({
    base: '/app/admin/component/'
}).use(['form', 'layer', 'laydate'], function(){
    var $ = layui.$
    ,form = layui.form
    ,layer = layui.layer
    ,laydate = layui.laydate;
    
    // 生日选择
    laydate.render({
        elem: '#birthday'
    });
    
    // 加载会员等级
    $.ajax({
        url: '/app/ky_shouyin/admin/member/getLevels'
        ,type: 'GET'
        ,success: function(res){
            if(res.code === 0){
                var html = '<option value="">请选择会员等级</option>';
                res.data.forEach(function(item){
                    html += '<option value="' + item.id + '">' + item.name + '</option>';
                });
                $('select[name="level"]').html(html);
                form.render('select');
            }
        }
    });
    
    // 加载门店列表
    $.ajax({
        url: '/app/ky_shouyin/admin/store/getList'
        ,type: 'GET'
        ,success: function(res){
            if(res.code === 0){
                var html = '<option value="">请选择所属门店</option>';
                res.data.forEach(function(item){
                    html += '<option value="' + item.id + '">' + item.name + '</option>';
                });
                $('select[name="store_id"]').html(html);
                form.render('select');
            }
        }
    });
    
    // 加载员工列表
    $.ajax({
        url: '/app/ky_shouyin/admin/staff/getList'
        ,type: 'GET'
        ,success: function(res){
            if(res.code === 0){
                var html = '<option value="">请选择办卡员工</option>';
                res.data.forEach(function(item){
                    html += '<option value="' + item.id + '">' + item.name + '</option>';
                });
                $('select[name="staff_id"]').html(html);
                form.render('select');
            }
        }
    });
    
    // 选择会员卡
    $('#selectCard').on('click', function(){
        layer.open({
            type: 2,
            title: '选择会员卡',
            area: ['800px', '600px'],
            content: '/app/ky_shouyin/admin/member/selectCard'
        });
    });
    
    // 监听提交
    form.on('submit(memberForm)', function(data){
        var field = data.field;
        
        // 提交数据
        $.ajax({
            url: '/app/ky_shouyin/admin/member/add'
            ,type: 'POST'
            ,data: field
            ,success: function(res){
                if(res.code === 0){
                    layer.msg('添加成功', {
                        icon: 1
                        ,time: 1000
                    }, function(){
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.layui.table.reload('LAY-member-list');
                    });
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }
        });
        
        return false;
    });
});
</script>
</body>
</html> 